<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="guanbi"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="submitRole">
        确定
      </el-button>
      <el-button size="small" @click="guanbi">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 导入 获取角色列表接口
import { getRoleList } from '@/api/setting.js'
// 获取用户信息
import { getUserDetailById } from '@/api/user'
// 导入 为用户分配角色
import { assignRoles } from '@/api/employees.js'

export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 选中角色的列表
      roleIds: [],
      // 角色列表
      list: [],
      // 当前用户ID
      userID: ''
    }
  },
  created () {
    this.xgetRoleLis()
  },
  methods: {
    guanbi () {
      this.$emit('update:showRoleDialog', false)
      this.roleIds = []
    },
    // 获取角色列表
    async xgetRoleLis () {
      const res = await getRoleList({ page: 1, pageSize: 1000 })
      console.log(res)
      this.list = res.rows
    },
    // 回显，回填数据
    async xgetUserDetailById (id) {
      this.userID = id
      const { roleIds } = await getUserDetailById(id)
      // console.log('用户信息：', roleIds)
      this.roleIds = roleIds
    },
    // 用户分配角色
    async submitRole () {
      await assignRoles({ id: this.userID, roleIds: this.roleIds })
      this.$message.success('操作成功！')
      this.guanbi()
    }
  }
}
</script>

<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      margin-bottom: 20px;
    }
  }
}
</style>
